home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / comm / ntmler8b.zip / GMAIL.CPP < prev    next >
C/C++ Source or Header  |  1996-09-16  |  17KB  |  537 lines

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <mbstring.h>
  5. #include "cgi.h"
  6. #include "gMail.h"
  7.  
  8.  
  9. CTemplateInfo::CTemplateInfo()
  10. {
  11.     m_pFormInformation = new FormContents;
  12.  
  13.     strcpy(m_pRawData, "");
  14.             
  15.     strcpy(m_pRespondMsg, "");
  16.     m_pMailFrom = m_pSendTo = m_pSucceedMsg = NULL;
  17.     m_pTemplateName = m_pSucceed = NULL;
  18.     m_pSMTPServer = m_pDefaultSender = NULL;
  19.     m_pErrTemplate = NULL;
  20.  
  21.     //Get the form data
  22.     cgiData.Initialize("POST");
  23.     //cgiData.SetContent("e_mail1=ivan_m_hendricks@ccm.hf.intel.com&send_to1=ivan_m_hendricks@ccm.hf.intel.com&send_to=ivanh@aracnet.com&name=Ivan M Hendricks&phone=357-0230&e_mail=ivanh@aracnet.com&submit=on&");
  24. }
  25.  
  26. CTemplateInfo::~CTemplateInfo()
  27. {
  28.     delete m_pFormInformation;
  29. }
  30.  
  31. void CTemplateInfo::SetTemplateFile(char* t)
  32. {
  33.     m_pTemplateName = t;
  34.     if (!LoadTemplateFile())
  35.         //product error because we could not load/find the file
  36.         HdrErr("Error loading template file", "Error occured loading the template file.  Please contact your system administrator");
  37.  
  38. }
  39.  
  40. ////////////////////////////////////////////////////////////////////////////////
  41. //Load the template file and dumps all the data into m_pRawData//
  42. ////////////////////////////////////////////////////////////////////////////////
  43. int CTemplateInfo::LoadTemplateFile()
  44. {
  45.     FILE* pFile;
  46.     char temptext[201];
  47.  
  48.     //Initialize temptext
  49.     strcpy(temptext, "");
  50.  
  51.     //Check to make sure the file exist, if not then error out, otherwise read the data
  52.     if((pFile = fopen(m_pTemplateName, "rt")) != NULL)
  53.     {
  54.         //Get all information from template file until we have reached the end (NULL)
  55.         //We are only allowing 500 characters per line.
  56.         while( (fgets(temptext, sizeof(temptext) - 1, pFile)) != NULL )
  57.             strcat(m_pRawData, temptext);    
  58.         return TRUE;
  59.     }
  60.     else
  61.         return FALSE;
  62. }
  63.  
  64. ///////////////////////////////////////////////////////////////////////////////
  65.  
  66. int CTemplateInfo::ParseTags()
  67. {
  68.  
  69.     m_pDefaultSender = GetTagData("<SENDER>", "</SENDER>", TRUE);
  70.     m_pSMTPServer = GetTagData("<SMTPSERVER>", "</SMTPSERVER>", TRUE);
  71.  
  72.     m_pErrTemplate = GetTagData("<ERRTEMPLATE>", "</ERRTEMPLATE>", TRUE);
  73.     m_pSubject = GetTagData("<SUBJECT>", "</SUBJECT>", TRUE);    
  74.     m_pSucceed = GetTagData("<SUCCEEDMSG>", "</SUCCEEDMSG>", TRUE);
  75.     
  76.     ParseFormData();
  77.  
  78.     return FALSE;
  79. }
  80.  
  81. ///////////////////////////////////////////////////////////////////////////////
  82.  
  83.  
  84. /**********************************************************************
  85. ** GetTagData - Send it the tag you are looking for and it returns  ***
  86. **                the data between the tag                            ***
  87. **********************************************************************/
  88. char* CTemplateInfo::GetTagData(char* BeginTag, char* EndTag, int required)
  89. {
  90.     char* pTagStart,
  91.         * pTagEnd,
  92.         * token = NULL;
  93.     int tokenlength = 0,
  94.         tokendifference = 0;
  95.     char errstr[200];
  96.     
  97.     //Check to make sure the tags are there
  98.     if ( stristr(m_pRawData, BeginTag) == NULL )
  99.         if (required)
  100.         {
  101.             sprintf(errstr, "%s is missing, please recheck your template file", BeginTag);
  102.             HdrErr("Tag missing", errstr);
  103.         }
  104.         else
  105.             return NULL;
  106.     if ( stristr(m_pRawData, EndTag) == NULL )
  107.         if (required)
  108.         {
  109.             sprintf(errstr, "%s is missing, please recheck your template file", EndTag);
  110.             HdrErr("Tag missing", errstr);
  111.         }
  112.         else
  113.             return NULL;
  114.     
  115.     //Parse out the data between the tags
  116.     pTagStart = stristr(m_pRawData, BeginTag);
  117.     pTagStart += strlen(BeginTag); //Move to the end ot the tag name
  118.     
  119.     pTagEnd = stristr(m_pRawData, EndTag);
  120.     tokendifference =  strlen(pTagStart) - strlen(pTagEnd);
  121.     token = new char[tokendifference + 1];
  122.  
  123.     strncpy(token, pTagStart, tokendifference);
  124.     strncpy(token + tokendifference, "\0", 1);
  125.  
  126.     return token;
  127.  
  128. }
  129. /////////////////////////////////////////////////////////////////////////////////
  130.  
  131. ////////////////////////////////////////////////////////////////////////////////
  132. void CTemplateInfo::ParseFormData()
  133. {
  134.     m_pRawFormData = GetTagData("<FORMDATA>", "</FORMDATA>", TRUE);    
  135.     SeperateFormInformation(m_pRawFormData);
  136.     GetFormData();    
  137.  
  138. }
  139.  
  140. ////////////////////////////////////////////////////////////////////////////////////
  141. //Based upon the information gathered from the template file, Find the values     //
  142. //stored in pCursor->pName and put the form variable contents into pCursor->pValue//
  143. ////////////////////////////////////////////////////////////////////////////////////
  144. void CTemplateInfo::GetFormData()
  145. {
  146.     FormContents* pCursor = m_pFormInformation;
  147.     
  148.     do
  149.     {
  150.         //Based on the variable name, put the value in pCursor->pValue
  151.         pCursor->pValue = cgiData.FindData(pCursor->pName, FALSE);
  152.         //Check to make sure that if it's a required field that it has some data in it.
  153.         if (pCursor->required) 
  154.             if (!pCursor->pValue)
  155.                     PrintErrHTML(pCursor->pReqErr);
  156.             else            
  157.             if ( strlen(pCursor->pValue) == 0) 
  158.                     PrintErrHTML(pCursor->pReqErr);
  159.         
  160.         //Advance to the next variable link
  161.         pCursor = pCursor->next;
  162.     }
  163.     while (pCursor != NULL);
  164.  
  165. }
  166.  
  167. ////////////////////////////////////////////////////////////////////////////////////////////
  168. //Based upon the infomation from the Template file, parse out the information in the      //
  169. // <formdata>...</formdata> tags and store the informatin in pCursor->                      //
  170. ////////////////////////////////////////////////////////////////////////////////////////////
  171. void CTemplateInfo::SeperateFormInformation(char RawData[])
  172. {
  173.     char* token;
  174.     FormContents* pCursor = m_pFormInformation;
  175.  
  176.     //Parse the form name information
  177.      token = strtok(RawData, "<");
  178.     token = strtok(NULL, ">");
  179.     do
  180.     {
  181.         //initialize variables
  182.         pCursor->pName = pCursor->pOutput = pCursor->pReqErr 
  183.                        = pCursor->pRespondMsg = pCursor->pValue = NULL;
  184.         pCursor->next = NULL;        
  185.  
  186.         GetNameToken(token, pCursor);
  187.         GetRequiredToken(token, pCursor);
  188.         GetSendToToken(token, pCursor);
  189.         GetRespondToToken(token, pCursor);
  190.         GetMailBodyToken(token, pCursor);
  191.  
  192.         //Go to the next section
  193.         token = strtok(NULL, "<");
  194.         token = strtok(NULL, ">");
  195.         if (token != NULL)
  196.         {
  197.             pCursor->next = new FormContents;
  198.             pCursor = pCursor->next;
  199.         }
  200.         //If we haven't reached the end, then create a new leaf for the next group of data
  201.     }while (token != NULL);
  202. }
  203.  
  204. /////////////////////////////////////////////////////////////////////////////////
  205. //Parse out the NAME= token and the associated value goes into pCursor->pName. //
  206. //The variable pCursor->pName is the name of the form variable                   //
  207. /////////////////////////////////////////////////////////////////////////////////
  208. void CTemplateInfo::GetNameToken(char* token, FormContents* pCursor)
  209. {
  210.     char* nameToken = NULL;
  211.     int stringsize = 0;
  212.     
  213.     pCursor->pName = NULL;
  214.     pCursor->pValue = NULL;
  215.     
  216.     nameToken = stristr(token, "NAME");
  217.  
  218.     //Make sure the 'name' tag exists
  219.     if (nameToken == NULL)
  220.         HdrErr("Bad command in template file", "Missing 'name' tag.  Please check template file");
  221.  
  222.     //find the '=' sign
  223.     nameToken = strstr(nameToken, "=");
  224.     //get the data between the double-quotes
  225.     nameToken = strstr(nameToken, "\"");
  226.     stringsize = strlen(++nameToken) - strlen (strstr(nameToken, "\""));
  227.     
  228.     pCursor->pName = new char[stringsize + 2];
  229.  
  230.     strncpy(pCursor->pName, nameToken, stringsize);
  231.     strncpy(pCursor->pName + stringsize, "=\0", 2);
  232.  
  233. }
  234.  
  235. /////////////////////////////////////////////////////////////////////////////////
  236. //Parse out the 'required' token.  If exists, also get 'requiredErr' token       //
  237. /////////////////////////////////////////////////////////////////////////////////
  238. void CTemplateInfo::GetRequiredToken(char token[], FormContents* pCursor)
  239. {
  240.     char* requiredErrToken = NULL;
  241.     int stringsize = 0;
  242.     char err[50] = "";
  243.     
  244.     //Parse the required boolean information.  If the 'required' is there, then 
  245.     //also get 'requiredErr' variable
  246.     if (stristr(token, "REQUIRED") != NULL)
  247.     {
  248.         pCursor->required = TRUE;
  249.         //Make sure the field 'requiredErr' exists.  If so, get the error message
  250.         if ( stristr(token, "REQUIREDERR")!= NULL)
  251.         {
  252.             requiredErrToken = stristr(token, "REQUIREDERR");
  253.             requiredErrToken = strstr(requiredErrToken, "=");
  254.             requiredErrToken = strstr(requiredErrToken, "\"");
  255.             stringsize = strlen(++requiredErrToken) - strlen (strstr(requiredErrToken, "\""));
  256.             if (stristr(token, "<") != NULL)
  257.             {
  258.                 sprintf(err, "Error on %s.  HTML tags are not allowed.  Use <errTemplate> tag below to customize your errors.", pCursor->pName);
  259.                 HdrErr("Error in template file", err);
  260.             }
  261.             pCursor->pReqErr = new char[stringsize];
  262.             strncpy(pCursor->pReqErr, requiredErrToken, stringsize);
  263.             strncpy(pCursor->pReqErr + stringsize, "\0", 1);
  264.         }
  265.         //The user specified 'required', but forgot to define a custom error message,
  266.         // so let's create a default message.  Not pretty, but it works.
  267.         else
  268.         {
  269.             pCursor->pReqErr = new char[70];
  270.             sprintf(pCursor->pReqErr, "%s is a required field. "
  271.                                      "Please go back", pCursor->pName);
  272.         }
  273.     }
  274.     //didn't find 'required'
  275.     else
  276.     {
  277.         pCursor->required = FALSE;
  278.         pCursor->pReqErr = NULL;
  279.     }
  280. }
  281. /////////////////////////////////////////////////////////////////////////////////
  282. //Get the 'sendto' token information                                           //
  283. /////////////////////////////////////////////////////////////////////////////////
  284. void CTemplateInfo::GetSendToToken(char token[], FormContents* pCursor)
  285. {
  286.     //Parse 'sendto' boolean information
  287.     if (stristr(token, "SENDTO") != NULL)
  288.         pCursor->sendto = TRUE;
  289.     else
  290.         pCursor->sendto = FALSE;
  291. }
  292.  
  293. /////////////////////////////////////////////////////////////////////////////////
  294. void CTemplateInfo::GetRespondToToken(char token[], FormContents* pCursor)
  295. {
  296.     char* respondToken = NULL;
  297.     int stringsize = 0;
  298.     char err[100] = "";
  299.  
  300.     //Default initialization
  301.     pCursor->respondto = FALSE;
  302.     pCursor->pRespondMsg = NULL;
  303.  
  304.     //Parse 'respondto' boolean information'
  305.     if (stristr(token, "RESPONDTO") != NULL)
  306.     {
  307.         //respondto and sendto cannot co-exist
  308.         if (pCursor->sendto)
  309.             HdrErr("Bad command in template file", "If there is a 'sendto' tag, then there cannot be a 'respondto' tag");
  310.  
  311.         pCursor->respondto = TRUE;
  312.         //Parse 'respondmsg' information
  313.         respondToken = stristr(token, "RESPONDMSG");
  314.         if (respondToken == NULL)
  315.         {
  316.             sprintf(err, "You specified a 'respondto' tag on %s with no corresponding 'respondmsg' tag", pCursor->pName);
  317.             HdrErr("Missing template data", err);
  318.         }
  319.         respondToken = strstr(respondToken, "=");
  320.         respondToken = strstr(respondToken, "\"");
  321.         stringsize = strlen(++respondToken) - strlen (strstr(respondToken, "\""));
  322.         pCursor->pRespondMsg = new char[stringsize];
  323.         strncpy(pCursor->pRespondMsg, respondToken, stringsize);
  324.         strncpy(pCursor->pRespondMsg + stringsize, "\0", 1);
  325.     }
  326. }
  327.  
  328. /////////////////////////////////////////////////////////////////////////////////
  329. //Get the mail body information (output).  Check a few conditions.               //
  330. //Condition 1:  If the 'sendto' tag is specified, then you cannot have an       //
  331. //              'output' tag                                                   //
  332. /////////////////////////////////////////////////////////////////////////////////
  333. void CTemplateInfo::GetMailBodyToken(char token[], FormContents* pCursor)
  334. {
  335.     char* outputToken = NULL;
  336.     int stringsize = 0;
  337.     char err[50] = "";
  338.  
  339.     //Default initialization
  340.     pCursor->pOutput = NULL;
  341.     
  342.     //Parse 'output' information
  343.     outputToken = stristr(token, "OUTPUT");
  344.     //If 'sendto' tag and 'output' tag is specified, then error out
  345.     if (pCursor->sendto && (outputToken != NULL))
  346.         HdrErr("Bad command in template file", "A 'sendto' tag and 'output' tag cannot co-exist");
  347.     //If 'output' token is not empty and 'sendto' is not specified, then error out
  348.     if (!pCursor->sendto)
  349.         if (outputToken != NULL) 
  350.         {
  351.             outputToken = strstr(outputToken, "=");
  352.             outputToken = strstr(outputToken, "\"");
  353.             stringsize = strlen(++outputToken) - strlen (strstr(outputToken, "\""));
  354.             pCursor->pOutput = new char[stringsize];
  355.             strncpy(pCursor->pOutput, outputToken, stringsize);
  356.             strncpy(pCursor->pOutput + stringsize, "\0", 1);
  357.         }
  358.         else
  359.         {
  360.             sprintf(err, "Missing the 'output' tag for %s'", pCursor->pName);
  361.             HdrErr("Bad command in template file", err);
  362.         }
  363. }
  364.  
  365. /////////////////////////////////////////////////////////////////////////////////
  366.  
  367. int CTemplateInfo::ParseSucceed()
  368. {
  369.     return TRUE;
  370. }
  371.  
  372.  
  373. ///////////////////////////////////////////////////////////////////////////////
  374. //Get the mail subject line                                                     //
  375. /////////////////////////////////////////////////////////////////////////////////
  376. int CTemplateInfo::ParseSubject()
  377. {
  378.     return TRUE;
  379. }
  380.  
  381. /////////////////////////////////////////////////////////////////////////////////
  382. //All the data that's stored in the link list (pCursor), dump all it's contents//
  383. //into a single variable that will, later, be dumped out to a file               //
  384. /////////////////////////////////////////////////////////////////////////////////
  385. void CTemplateInfo::GenerateMailToFile()
  386. {
  387.     FormContents* pCursor = m_pFormInformation;
  388.  
  389.     strcpy(m_pMailInfoTo, "");
  390.     strcpy(m_pRespondMsg, "");
  391.     do
  392.     {
  393.         //Write info to variable to be put in mail file.
  394.         if (pCursor->pOutput != NULL) 
  395.             sprintf(m_pMailInfoTo, "%s %s %s \n", m_pMailInfoTo, pCursor->pOutput, pCursor->pValue);
  396.         //Advance to the next variable link
  397.         pCursor = pCursor->next;
  398.     }
  399.     while (pCursor != NULL);
  400.     
  401. }
  402.  
  403. /////////////////////////////////////////////////////////////////////////////////
  404. //Mail all the information to all the 'sendto' tagged people and send out the  //
  405. //'respondto' tag data to the respective people.  
  406. /////////////////////////////////////////////////////////////////////////////////
  407. void CTemplateInfo::MailInformation()
  408. {
  409.     char buf[500];
  410.     char *pTempFileName;        //Holds the temp filename used to hold the body of the message
  411.     char BlatExec[] = "blat.exe";
  412.     char param1[50] = "",
  413.          param2[50] = "",
  414.          param3[50] = "",
  415.          param4[50] = "";
  416.     FormContents* pCursor = m_pFormInformation;
  417.         
  418.     GenerateMailToFile();
  419.     pTempFileName = CreateTempFile(m_pMailInfoTo);
  420.     //Flush the stream and buffers.  Docs say this must be done before system(..) is called
  421.     _flushall();
  422.  
  423.     do
  424.     {
  425.         //Generate a commandline to call the blat program and send the contents of the temporary file
  426.         if (pCursor->sendto)
  427.         {
  428.             sprintf(param1, "%s", pTempFileName); //file to send with the contents of message
  429.             sprintf(param2, "-s \"%s\"", m_pSubject); // Create the subject line
  430.             sprintf(param3, "-f \"%s\" -server \"%s\"", m_pDefaultSender, m_pSMTPServer);
  431.             //space for param3
  432.             sprintf(param4, "-t %s > send.log", pCursor->pValue); //create the mailto line;
  433.  
  434.             //mail file from command-line
  435.             sprintf(buf, "%s %s %s %s %s", BlatExec, param1, param2, param3, param4);
  436.             
  437.             //spawn off a process to call blat and mail the file.
  438.             system(buf); 
  439.         }
  440.  
  441.         pCursor = pCursor->next;
  442.     } while (pCursor != NULL);            
  443.  
  444.  
  445.     //Cleanup and delete the temporary file
  446.     DeleteFile(pTempFileName);
  447.  
  448.     //Now do the Mailback to person filling out form
  449.     pCursor = m_pFormInformation;
  450.     do
  451.     {
  452.         if (pCursor->respondto)
  453.         {
  454.             pTempFileName = CreateTempFile(pCursor->pRespondMsg);
  455.             
  456.             //reversed from and to so the original sender of the form gets a confirmation request.  This is
  457.             //used to verify that the user sent the correct e-mail address.
  458.             sprintf(buf, "blat.exe %s -s \"%s\" -t %s %s>respond.log", pTempFileName, m_pSubject, _strlwr(pCursor->pValue), param3 );         
  459.             _flushall();
  460.             system(buf);
  461.  
  462.             //Cleanup and delete the temporary file
  463.             DeleteFile(pTempFileName);
  464.         }
  465.         pCursor = pCursor->next;
  466.  
  467.     } while(pCursor != NULL);
  468.  
  469.     PrintSucceedMsg();
  470. }
  471.  
  472. /////////////////////////////////////////////////////////////////////////////////
  473. //This is the HTML Success message
  474. /////////////////////////////////////////////////////////////////////////////////
  475. void CTemplateInfo::PrintSucceedMsg()
  476. {
  477.     PrintHeader("Thankyou");
  478.     printf(m_pSucceed);
  479. }
  480.  
  481. char* CreateTempFile(const char* pFileContents)
  482. {
  483.     FILE* pOutputFile;            //File stream that data is written to
  484.     char* pTempFileName = NULL;
  485.  
  486.     //Create a temporary file to contain the information to send through blat
  487.     //GetTempPath( sizeof(pTempDir), pTempDir );
  488.     
  489.     pTempFileName = new char[15];
  490.     GetTempFileName( ".\\", "cml", 0, pTempFileName );
  491.  
  492.     if ( (pOutputFile = fopen(pTempFileName,"w") ) == NULL)
  493.     {
  494.         //Couldn't file the file
  495.         HdrErr("File Creation error", "Error occured while creating mail file.");
  496.         exit(0);
  497.     }    
  498.     fputs ("\n", pOutputFile);
  499.     fputs (pFileContents, pOutputFile);
  500.     fputs ("\n", pOutputFile);
  501.     fclose(pOutputFile);
  502.  
  503.     return pTempFileName;
  504. }
  505.  
  506.  
  507.  
  508. void HdrErr(char* pErrorHeader, char* pErrorText)
  509. {
  510.     PrintHeader(pErrorHeader);
  511.     printf(pErrorText);
  512.     exit(0);
  513. }
  514.  
  515.  
  516.  
  517. void CTemplateInfo::PrintErrHTML(char* errstring)
  518. {
  519.     char* temp = NULL;
  520.     char* errtag = NULL;
  521.     int j = 0;
  522.  
  523.     PrintHeader("Required field missing information");
  524.  
  525.     errtag = stristr(m_pErrTemplate, "$err$");
  526.     if (errtag == NULL)
  527.         err(m_pErrTemplate);
  528.     for (char* i= m_pErrTemplate; i < errtag; i++)
  529.     {
  530.         printf("%c", *i);
  531.     }
  532.     errtag += 5;
  533.     printf("%s", errstring);
  534.     printf(errtag);
  535.     exit(0);
  536. }
  537.